#ifdef GetMessage
#undef GetMessage
#endif

#ifndef wxyutils_h_
#define wxyutils_h_

#include <pfcGlobal.h>
#include <uifcDefaultListeners.h>
#include <pfcExceptions.h>
#include <OTKXString.h>
#include <vector>
#include <logger.h>
#include <wfcClient.h>
class _FileExt
{
public:
    string fullname;  //绝对路径
    string filename;  //文件名(不含扩展名)
    string extension; //扩展名
    string filepath;  //绝对路径(不含文件名)
    string version;   //数字扩展名

public:
    _FileExt(const wstring &file);
    _FileExt(const char *file);
};

//判断模型环境
int CheckEnv(int count, ...);
//根据类型获取目录下所有文件
// void ListFileByType(string path, string match, vector<_FileExt> &files);
void ListFileByType(const char *path, const char *match, vector<_FileExt> &files);
//文件排序
bool sortbyname(const _FileExt &t1, const _FileExt &t2);
//清理小版本
void MyPurge(string path);
//获取Dll目录
extern "C" _declspec(dllexport) int GetDllPath(string &Path);
extern "C" typedef wfcStatus (*myCallback)();
class MycallbackClass : public virtual pfcUICommandActionListener
{
public:
    MycallbackClass(myCallback func) { m_func = func; }

    void OnCommand()
    {
        m_func();
    }

private:
    myCallback m_func;
};
class MyActionAccess : public virtual pfcUICommandAccessListener
{
public:
    MyActionAccess(pfcCommandAccess CommandAccess) { m_CommandAccess = CommandAccess; }
    pfcCommandAccess OnCommandAccess(xbool AllowErrorMessages)
    {
        return m_CommandAccess;
    }

private:
    pfcCommandAccess m_CommandAccess;
};
//创建UICommand
pfcUICommand_ptr CreateUICommand(pfcSession_ptr Session,
                                 xrstring Name,
                                 pfcUICommandActionListener_ptr Action,
                                 xrstring MessageFile,
                                 xrstring Label,
                                 optional xrstring Help,
                                 optional xrstring Description,
                                 pfcActionListener_ptr Listener,
                                 xrstring IconFile = NULL);

//关闭会话窗口
class CloseDialogListener : public uifcDefaultDialogListener
{
public:
    void OnClose(uifcDialog_ptr handle)
    {
        uifcExitDialog(handle->GetDialog(), 0);
        LOG(LOG_INFO) << "Dialog is closed!";
    }
    void OnDestroy(uifcDialog_ptr handle)
    {
        LOG(LOG_INFO) << "Dialog is destoried";
    }
};
class CloseButton : public uifcDefaultPushButtonListener
{
public:
    void OnActivate(uifcPushButton_ptr handle)
    {
        uifcExitDialog(handle->GetDialog(), 0);
    }
};

//异常处理

/* Prints exception to error.log */
#define OTK_EXCEPTION_PRINT(pfcex)                                                                          \
    {                                                                                                       \
        xstring msg = pfcex->GetMessage();                                                                  \
        xstring method = pfcex->GetMethodName();                                                            \
        xint err = pfcex->GetErrorCode();                                                                   \
        LOG(LOG_ERROR) << "pfcXToolkitError in method " << method << " with error " << err << "\n message " \
                       << msg;                                                                              \
    }

/* Prints exception to error.log */
#define OTK_DEFAULT_EXCEPTION_PRINT(ftypeex)                   \
    {                                                          \
        xstring msg = ftypeex->getCipTypeName();               \
        LOG(LOG_ERROR) << "Uncaught " << msg << " exception "; \
    }

/* Prints invalid file info to error.log */
#define OTK_FILENAME_EXCEPTION_PRINT(fname)             \
    {                                                   \
        xstring msg = fname->GetMessage();              \
        LOG(LOG_ERROR) << "Invalid File type: " << msg; \
    }

/* Prints invalid mdl extension info to error.log */
#define OTK_MDLEXT_EXCEPTION_PRINT(mdlext)              \
    {                                                   \
        xstring msg = mdlext->GetMessage();             \
        LOG(LOG_ERROR) << "Invalid File type: " << msg; \
    }

/* Prints uifcError info to error.log */
#define OTK_UIFCERROR_EXCEPTION_PRINT(uifcerror)     \
    {                                                \
        xstring msg = uifcerror->GetMessage();       \
        xstring com = uifcerror->GetComponent();     \
        xstring dialog = uifcerror->GetDialog();     \
        LOG(LOG_ERROR) << "uifcError:\nmsg: " << msg \
                       << "\nComponent: " << com     \
                       << "\nDialog: " << dialog;    \
    }

#define OTK_EXCEPTION_PRINT_LOG                                                            \
    xcatchbegin                                                                            \
        xcatch(pfcXToolkitError, pfcex){                                                   \
            OTK_EXCEPTION_PRINT(pfcex)} xcatch(pfcXInvalidFileName, pfcex){                \
            OTK_FILENAME_EXCEPTION_PRINT(pfcex)} xcatch(pfcXUnknownModelExtension, pfcex){ \
            OTK_MDLEXT_EXCEPTION_PRINT(pfcex)} xcatch(uifcError, pfcex){                   \
            OTK_UIFCERROR_EXCEPTION_PRINT(pfcex)} xcatchcip(pfcex){                        \
            OTK_DEFAULT_EXCEPTION_PRINT(pfcex)} xcatchend
#endif